package com.cyam.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cyam.domain.Article;
import com.cyam.domain.ArticleType;
import com.cyam.service.ArticleService;
import com.cyam.service.ArticleTypeService;
import com.cyam.util.Pager;
import com.cyam.util.TimeUtil;

/**
 * 中间内容
 * @author CourageAQ
 *
 */
@Controller
@RequestMapping("/main")
public class MainController {
	@Resource
	private ArticleService articleService;
	
	@Resource
	private ArticleTypeService articleTypeService;
	
	/**
	 * 主页根据点击量排序，返回数据
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/list")
	@ResponseBody
	public JSONObject viewContent(@RequestParam(value = "page",defaultValue = "1") int page,
			@RequestParam(value ="size",defaultValue = "8") int size) throws Exception {
		JSONObject result = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		Pager pages = articleService.findPager(map, page, size);
		result.put("result", pages);
		return result;
	}
	
	/**
	 * 跳转到详情解码
	 */
	@RequestMapping("/index/detail")
	public String viewDetail(Model model,@RequestParam(value = "id") int id) throws Exception {
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("id", id);
		Article ac = new Article();
		Article article = articleService.findOne(map);
		//点击量加一
		ac.setId(id);
		articleService.update(ac);
		model.addAttribute("article", article);
		return "index-detail";
	}
	
	
	
	
	/***
	 * 最近更新
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/lately")
	public String viewLately(Model model) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("article_time", TimeUtil.currentOldTwoMonth());
		Pager pages = articleService.countPager3(map,1, 8);
		model.addAttribute("pages", pages);
		return "lately";
	}
	/**
	 * 最近更新根据时间返回，大于两个月之前的时间返回
	 * @param page
	 * @param size
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/latelylist")
	@ResponseBody
	public JSONObject viewLatelyContent(@RequestParam(value = "page",defaultValue = "1") int page,
			@RequestParam(value ="size",defaultValue = "8") int size) throws Exception {
		JSONObject result = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("article_time", TimeUtil.currentOldTwoMonth());
		Pager pages = articleService.selectPage3(map, page, size);
		result.put("result", pages);
		return result;
	}
	
	/**
	 * 博客文章
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/blog")
	public String viewBlog(Model model) throws Exception {
		Pager pages = articleService.countPager(null,1, 8);
		model.addAttribute("pages", pages);
		return "blog";
	}
	
	/**
	 * 博客文章 按时间排序
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/bloglist")
	@ResponseBody
	public JSONObject viewBlogList(@RequestParam(value = "page",defaultValue = "1") int page,
			@RequestParam(value ="size",defaultValue = "8") int size) throws Exception {
		JSONObject result = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("article_time", "article_time");
		Pager pages = articleService.findPager(map, page, size);
		result.put("result", pages);
		return result;
	}
	
	
	
	
	/**博客分类
	 * @throws Exception */
	@RequestMapping("/type")
	public String articleType(Model model) throws Exception {
		List<ArticleType> list = articleTypeService.findAll(null);
		model.addAttribute("article", list);
		return "type";
	}
	
	
	
	
	
	
}
